{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "4f70b76b",
      "metadata": {
        "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
        "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
        "execution": {
          "iopub.execute_input": "2021-10-28T10:18:49.232672Z",
          "iopub.status.busy": "2021-10-28T10:18:49.227561Z",
          "iopub.status.idle": "2021-10-28T10:19:04.502716Z",
          "shell.execute_reply": "2021-10-28T10:19:04.503216Z",
          "shell.execute_reply.started": "2021-10-28T10:08:45.550332Z"
        },
        "papermill": {
          "duration": 15.287961,
          "end_time": "2021-10-28T10:19:04.503522",
          "exception": false,
          "start_time": "2021-10-28T10:18:49.215561",
          "status": "completed"
        },
        "tags": [],
        "id": "4f70b76b",
        "outputId": "224a93fa-0626-4354-837b-cba9c4ad5f4c",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (1.5.2)\n",
            "Requirement already satisfied: numpy>=1.19.5 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (1.26.4)\n",
            "Requirement already satisfied: scipy>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (1.13.1)\n",
            "Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (1.4.2)\n",
            "Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn) (3.5.0)\n"
          ]
        }
      ],
      "source": [
        "%pip install scikit-learn -U"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "4b69c03e",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-10-28T10:19:04.545681Z",
          "iopub.status.busy": "2021-10-28T10:19:04.544725Z",
          "iopub.status.idle": "2021-10-28T10:19:05.501277Z",
          "shell.execute_reply": "2021-10-28T10:19:05.500590Z",
          "shell.execute_reply.started": "2021-10-28T10:09:03.182001Z"
        },
        "papermill": {
          "duration": 0.979723,
          "end_time": "2021-10-28T10:19:05.501434",
          "exception": false,
          "start_time": "2021-10-28T10:19:04.521711",
          "status": "completed"
        },
        "tags": [],
        "id": "4b69c03e"
      },
      "outputs": [],
      "source": [
        "from sklearn.datasets import make_classification\n",
        "from sklearn.model_selection import train_test_split\n",
        "\n",
        "X, y = make_classification(n_samples=300, n_features=50,\n",
        "                           n_informative=10,\n",
        "                           n_redundant=25, n_repeated=15,\n",
        "                           n_clusters_per_class=5,\n",
        "                           flip_y=0.05, class_sep=0.5,\n",
        "                           random_state=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "62513810",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-10-28T10:19:05.542295Z",
          "iopub.status.busy": "2021-10-28T10:19:05.541309Z",
          "iopub.status.idle": "2021-10-28T10:19:05.563842Z",
          "shell.execute_reply": "2021-10-28T10:19:05.563327Z",
          "shell.execute_reply.started": "2021-10-28T10:09:04.087478Z"
        },
        "papermill": {
          "duration": 0.045382,
          "end_time": "2021-10-28T10:19:05.563986",
          "exception": false,
          "start_time": "2021-10-28T10:19:05.518604",
          "status": "completed"
        },
        "tags": [],
        "id": "62513810"
      },
      "outputs": [],
      "source": [
        "from sklearn import svm\n",
        "\n",
        "svc = svm.SVC()\n",
        "svc = svm.SVC(probability=True, random_state=1)\n",
        "\n",
        "from sklearn import model_selection\n",
        "search_grid = [\n",
        "               {'C': [1, 10, 100, 1000], 'kernel': ['linear']},\n",
        "               {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001],\n",
        "               'kernel': ['rbf']}\n",
        "               ]\n",
        "\n",
        "scorer = 'accuracy'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "b949dbdf",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-10-28T10:19:05.604674Z",
          "iopub.status.busy": "2021-10-28T10:19:05.603884Z",
          "iopub.status.idle": "2021-10-28T10:34:48.628686Z",
          "shell.execute_reply": "2021-10-28T10:34:48.629211Z"
        },
        "papermill": {
          "duration": 943.048701,
          "end_time": "2021-10-28T10:34:48.629569",
          "exception": false,
          "start_time": "2021-10-28T10:19:05.580868",
          "status": "completed"
        },
        "tags": [],
        "id": "b949dbdf",
        "outputId": "db6bb31f-7677-4b49-9075-7b2c98ef82ce",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'C': 10, 'gamma': 0.001, 'kernel': 'rbf'}\n",
            "0.7\n"
          ]
        }
      ],
      "source": [
        "search_func = model_selection.GridSearchCV(estimator=svc,\n",
        "                                           param_grid=search_grid,\n",
        "                                           scoring=scorer,\n",
        "                                           n_jobs=-1,\n",
        "                                           cv=5)\n",
        "search_func.fit(X, y)\n",
        "\n",
        "print (search_func.best_params_)\n",
        "print (search_func.best_score_)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "98d9aa9d",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-10-28T10:34:48.670290Z",
          "iopub.status.busy": "2021-10-28T10:34:48.669534Z",
          "iopub.status.idle": "2021-10-28T10:42:04.936145Z",
          "shell.execute_reply": "2021-10-28T10:42:04.936654Z"
        },
        "papermill": {
          "duration": 436.289573,
          "end_time": "2021-10-28T10:42:04.936849",
          "exception": false,
          "start_time": "2021-10-28T10:34:48.647276",
          "status": "completed"
        },
        "tags": [],
        "id": "98d9aa9d",
        "outputId": "40469c82-e0c9-4839-9878-ac132f11201e",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'C': 34.722337586969275, 'gamma': 0.0005003933743157808, 'kernel': 'rbf'}\n",
            "0.7133333333333333\n"
          ]
        }
      ],
      "source": [
        "import scipy.stats as stats\n",
        "\n",
        "search_dict = {'kernel': ['linear', 'rbf'],\n",
        "               'C': stats.loguniform(1, 1000),\n",
        "               'gamma': stats.loguniform(0.0001, 0.1)\n",
        "               }\n",
        "\n",
        "scorer = 'accuracy'\n",
        "\n",
        "search_func = model_selection.RandomizedSearchCV(estimator=svc,\n",
        "                                                 param_distributions=search_dict,\n",
        "                                                 n_iter=6,\n",
        "                                                 scoring=scorer,\n",
        "                                                 n_jobs=-1,\n",
        "                                                 cv=5\n",
        "                                                 )\n",
        "\n",
        "search_func.fit(X, y)\n",
        "\n",
        "print (search_func.best_params_)\n",
        "print (search_func.best_score_)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "72e2b127",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-10-28T10:42:04.981942Z",
          "iopub.status.busy": "2021-10-28T10:42:04.981239Z",
          "iopub.status.idle": "2021-10-28T10:43:10.549111Z",
          "shell.execute_reply": "2021-10-28T10:43:10.549612Z"
        },
        "papermill": {
          "duration": 65.594403,
          "end_time": "2021-10-28T10:43:10.549795",
          "exception": false,
          "start_time": "2021-10-28T10:42:04.955392",
          "status": "completed"
        },
        "tags": [],
        "id": "72e2b127",
        "outputId": "f92889d1-293a-48cb-b9c5-d687aa100f33",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'C': 86.63279761354555, 'gamma': 0.002054762512591136, 'kernel': 'linear'}\n",
            "0.6166666666666667\n"
          ]
        }
      ],
      "source": [
        "from sklearn.experimental import enable_halving_search_cv\n",
        "from sklearn.model_selection import HalvingRandomSearchCV\n",
        "\n",
        "search_func =  HalvingRandomSearchCV(estimator=svc,\n",
        "                                     param_distributions=search_dict,\n",
        "                                     resource='n_samples',\n",
        "                                     max_resources=100,\n",
        "                                     aggressive_elimination=True,\n",
        "                                     scoring=scorer,\n",
        "                                     n_jobs=-1,\n",
        "                                     cv=5,\n",
        "                                     random_state=0)\n",
        "\n",
        "search_func.fit(X, y)\n",
        "\n",
        "print (search_func.best_params_)\n",
        "print (search_func.best_score_)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.10"
    },
    "papermill": {
      "default_parameters": {},
      "duration": 1470.658665,
      "end_time": "2021-10-28T10:43:11.481625",
      "environment_variables": {},
      "exception": null,
      "input_path": "__notebook__.ipynb",
      "output_path": "__notebook__.ipynb",
      "parameters": {},
      "start_time": "2021-10-28T10:18:40.822960",
      "version": "2.3.3"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}